package com.xiexiaozhou.helper.web.controller;

import com.xiexiaozhou.helper.core.domain.Customer;
import com.xiexiaozhou.helper.core.service.CustomerService;
import com.xiexiaozhou.helper.web.from.CustomerForm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 公开的控制器,所有不需要身份认证的方法都会放到这个控制器中
 */
@Controller
@RequestMapping("/publiz")
public class PublizController {

    @Autowired
    public CustomerService customerService;

    /**
     * 进入注册页面
     *
     * @return
     */
    @GetMapping("/register")
    public String register() {
        return "publiz/register";
    }

    /**
     * 执行注册操作
     *
     * @param customerForm
     * @param bindingResult
     * @param model
     * @return
     */
    @PostMapping("/register")
    public String register(@Validated CustomerForm customerForm,
                           BindingResult bindingResult,
                           HttpSession session,
                           Model model) {
        //清除session中已存在的注册信息
        if (session.getAttribute("temporary") != null) {
            session.removeAttribute("temporary");
        }

        //将注册信息保存到session中
        session.setAttribute("temporary", customerForm);

        //表单验证
        if (bindingResult.hasErrors()) {
            String errorMsg = "";
            List<ObjectError> objectErrorList = bindingResult.getAllErrors();
            for (ObjectError error : objectErrorList) {
                errorMsg = errorMsg + error.getDefaultMessage() + " ";
            }
            model.addAttribute("errorMsg", errorMsg);
            return "/publiz/register";
        }

        //对象赋值
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerForm, customer);

        //调用注册方法
        Customer customer1 = customerService.register(customer);

        //注册成功
        if (customer1 != null) {
            model.addAttribute("message", "注册成功，请登录！");
            return "/publiz/login";
        }

        return null;
    }


    /**
     * 进入登录界面
     *
     * @return
     */
    @GetMapping("/login")
    public String login() {
        return "publiz/login";
    }

    /**
     * 执行登录操作
     *
     * @param username
     * @param password
     * @param session
     * @param model
     * @return
     */
    @PostMapping("/login")
    public String login(
            @RequestParam String username,
            @RequestParam String password,
            HttpSession session,
            Model model) {
        Customer customer = customerService.login(username, password);
        if (customer == null) {
            //登录失败
            //重新跳转到登录界面，并给出相关提示
            model.addAttribute("errorMsg", "用户名或密码不正确，请重新输入！");
            return "/publiz/login";
        } else {
            //登录成功
            //将用户信息存放到session
            customer.setPassword(null);
            session.setAttribute("customer", customer);

            //重定向到个人中心
            return "redirect:/customer/index";
        }
    }

    /**
     * 进入关于我们页面
     *
     * @return
     */
    @GetMapping("/about")
    public String about() {
        return "/publiz/about";
    }
}

